package com.yifan.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class BaseDao {

	public void addObject(Connection conn, String sql, Object params[]) throws SQLException {
        QueryRunner qr = new QueryRunner();
        qr.update(conn, sql, params);
        conn.close();
	}
	
	public long addObjectReturnId(Connection conn, String sql, Object params[]) throws SQLException {
        QueryRunner qr = new QueryRunner();
        qr.update(conn, sql, params);
        BigInteger num= (BigInteger) qr.query(conn,"select @@identity",new ScalarHandler(1));
        conn.close();
        return num.longValue();
	}
	
	public Object getObject(Connection conn, String sql, Class type, Object[] params) throws SQLException {
		QueryRunner qr = new QueryRunner();
		Object obj = qr.query(conn, sql, new BeanHandler(type), params);
		conn.close();
        return obj;
	}
	
	public Map<String,Object> getObjectRMap(Connection conn, String sql, Object[] params) throws SQLException {
		QueryRunner qr = new QueryRunner();
		Map<String,Object> obj = qr.query(conn, sql, new MapHandler(), params);
		conn.close();
        return obj;
	}
	
	public List<Object> getObjects(Connection conn, String sql, Class type, Object[] params) throws SQLException {
		QueryRunner qr = new QueryRunner();
		List<Object> list = (List<Object>)qr.query(conn, sql, new BeanListHandler(type), params);
		conn.close();
        return list;
	}
	
//	List<Map<String,Object>> mapList = qr.query(sql, new MapListHandler());
	public List<Object> getObjects(Connection conn, String sql, Class type, List<Object> list) throws SQLException {
		Object[] params = new Object[list.size()];
		for(int i = 0;i < list.size();i++){
			params[i] = list.get(i);
		}
		List<Object> nlist = getObjects(conn, sql, type, params);
		
		conn.close();
		return nlist;
	}
	
	public List<Map<String,Object>> getObjectsRMap(Connection conn, String sql, List<Object> list) throws SQLException {
		Object[] params = new Object[list.size()];
		for(int i = 0;i < list.size();i++){
			params[i] = list.get(i);
		}
		QueryRunner qr = new QueryRunner();
		return qr.query(conn, sql, new MapListHandler(),params);
	}
	
	public List<Object> getObjectsBys(Connection conn, String sql, Class type, Object[] params) throws SQLException {
		QueryRunner qr = new QueryRunner();
		
		List<Object> nlist = (List<Object>)qr.query(conn, sql, new BeanListHandler(type), params);
        return nlist;
	}
	
	public void deleteObject(Connection conn, String sql, Object[] param) throws SQLException {
		QueryRunner qr = new QueryRunner();
		qr.update(conn, sql, param);
		conn.close();
	}
	
	public void updateObject(Connection conn, String sql, Object[] param) throws SQLException {
		QueryRunner qr = new QueryRunner();
		qr.update(conn, sql, param);
		conn.close();
	}
	
	public int countObject(Connection conn, String sql, Object[] param) throws SQLException {
		QueryRunner qr = new QueryRunner();
        Number cnt = (Number)qr.query(conn, sql, new ScalarHandler(), param);
        conn.close();
        return cnt.intValue();
	}
	
	public int countObject(Connection conn, String sql,List<Object> list) throws SQLException {
		Object[] params = new Object[list.size()];
		for(int i = 0;i < list.size();i++){
			params[i] = list.get(i);
		}
		int count = countObject(conn, sql, params);
        return count;
	}
}
